package cn.getech.data.development.controller.api;


import cn.getech.data.development.api.JobNodeRunHistoryApi;
import cn.getech.data.development.constant.LastRunState;
import cn.getech.data.development.entity.JobNodeRunHistory;
import cn.getech.data.development.entity.JobRunHistory;
import cn.getech.data.development.service.JobNodeRunHistoryService;
import cn.getech.data.development.service.JobRunHistoryService;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName JobNodeInfoApiController
 * @Description TODO
 * @Author Getech
 * @Date 2020/12/24 11:46
 */
@RestController
public class JobNodeRunHistoryApiController implements JobNodeRunHistoryApi {

    @Autowired
    private JobNodeRunHistoryService jobNodeRunHistoryService;
    @Autowired
    private JobRunHistoryService jobRunHistoryService;

    @Override
    public JobNodeRunHistory getOne(JobNodeRunHistory jobNodeRunHistorySerachData) {
        QueryWrapper<JobNodeRunHistory> jobNodeRunHistoryQueryWrapper = new QueryWrapper<>();
        if(null != jobNodeRunHistorySerachData){
            jobNodeRunHistoryQueryWrapper.eq(StringUtils.isNotEmpty(jobNodeRunHistorySerachData.getOozieJobId()),"oozie_job_id",jobNodeRunHistorySerachData.getOozieJobId());
            jobNodeRunHistoryQueryWrapper.eq(null != jobNodeRunHistorySerachData.getJobNodeInfoId(),"job_node_info_id",jobNodeRunHistorySerachData.getJobNodeInfoId());
            return jobNodeRunHistoryService.getOne(jobNodeRunHistoryQueryWrapper);
        }
        return null;
    }

    @Override
    public void saveOrUpdate(JobNodeRunHistory jobNodeRunHistory) {
        //先查询对应的节点信息
        if(null != jobNodeRunHistory && null != jobNodeRunHistory.getId()){
            JobNodeRunHistory jobNodeRunHistory1 = jobNodeRunHistoryService.getById(jobNodeRunHistory.getId());
            //判断对象的字段是否超时
            if(null != jobNodeRunHistory1 && Objects.equals(jobNodeRunHistory1.getIsOutHours(),1)){
                jobNodeRunHistory1.setIsOutHours(1);
            }
            //判断是否是停止状态(则不修改)
            if(null != jobNodeRunHistory1 && Objects.equals(jobNodeRunHistory1.getState(), LastRunState.STOP.getCode())){
                jobNodeRunHistory.setState(LastRunState.STOP.getCode());
            }else{
                //判断这个oozieId 对应的状态
                List<JobRunHistory> list = jobRunHistoryService.list(new LambdaQueryWrapper<JobRunHistory>().eq(JobRunHistory::getOozieJobId, jobNodeRunHistory.getOozieJobId()));
                if(CollectionUtil.isNotEmpty(list)){
                    if(Objects.equals(list.get(0).getState(),LastRunState.STOP.getCode())){
                        jobNodeRunHistory.setState(LastRunState.STOP.getCode());
                    }
                }
            }
        }
        jobNodeRunHistoryService.saveOrUpdate(jobNodeRunHistory);
    }
}
